package com.graduation.table.clazz.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation.base.Result;
import com.graduation.table.clazz.ClazzVo;
import com.graduation.table.clazz.entity.Clazz;
import com.graduation.table.clazz.service.IClazzService;
import com.graduation.table.company.entity.Company;
import com.graduation.util.MinioUtil;
import com.graduation.util.PageUtil;
import com.graduation.util.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 班级表
 * @Author: jeecg-boot
 * @Date:   2022-04-08
 * @Version: V1.0
 */
@Slf4j
@Api(tags="班级表")
@RestController
@RequestMapping("/clazz")
public class ClazzController {
	@Resource
	private IClazzService clazzService;
	@Resource
	private MinioUtil minioUtil;
	
	/**
	 * 分页列表查询
	 *
	 * @param clazz
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="班级表-分页列表查询", notes="班级表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Clazz clazz,
								   String search,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(search), Clazz::getName,search)
				.or()
				.like(!StringUtils.isEmpty(search),Clazz::getInstructor,search)
		;
		List<Clazz> list = clazzService.list(queryWrapper);
		PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
		return Result.OK(pageUtil);
	}
	
	/**
	 * 添加
	 *
	 * @param clazz
	 * @return
	 */
	@ApiOperation(value="班级表-添加", notes="班级表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Clazz clazz) {
		clazzService.save(clazz);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param clazz
	 * @return
	 */
	@ApiOperation(value="班级表-编辑", notes="班级表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Clazz clazz) {

		clazz.setImages(StringUtil.arrayToString(clazz.getImgList()));
		clazzService.updateById(clazz);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="班级表-通过id删除", notes="班级表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		clazzService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="班级表-批量删除", notes="班级表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.clazzService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="班级表-通过id查询", notes="班级表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Clazz clazz = clazzService.getById(id);
		return Result.OK(clazz);
	}
	@ApiOperation("上传客户端图片")
	@ApiImplicitParam(name = "上传客户端图片", value = "上传客户端图片  ")
	@PostMapping("/upload")
	public Result<?> uploadPhoto(@RequestParam("file") MultipartFile file) throws IOException {
		try {
			int length = file.getBytes().length;
			if (length > 5 * 1024 * 1024){
				return Result.error("图片不得超过5M");
			}
		} catch (IOException e) {
			e.printStackTrace();
			return Result.error("上传文件失败");
		}
		long time1 = System.currentTimeMillis();
		int begin = Objects.requireNonNull(file.getOriginalFilename()).indexOf(".");
		int last = file.getOriginalFilename().length();
		//获得文件后缀名
		String a = file.getOriginalFilename().substring(begin, last);
		String fileName = file.getName()+time1+a;
		boolean upload = minioUtil.putObject("graduation", fileName, file.getInputStream());
		if (upload){
			return Result.OK(fileName);
		}else {
			return Result.error("上传失败");
		}
	}

	@ApiOperation("下载图片")
	@ApiImplicitParam(name = "下载图片", value = "下载图片  ")
	@GetMapping("/download")
	public void downloadPhoto(String fileName, HttpServletResponse response) throws IOException {
		InputStream in = minioUtil.getObject("graduation",fileName);

		OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
		//创建存放文件内容的数组
		byte[] buff =new byte[1024];
		//所读取的内容使用n来接收
		int n;
		//当没有读取完时,继续读取,循环
		while((n=in.read(buff))!=-1){
			//将字节数组的数据全部写入到输出流中
			outputStream.write(buff,0,n);
		}
		//强制将缓存区的数据进行输出
		outputStream.flush();
		//关流
		outputStream.close();
		in.close();
	}

	@ApiOperation("查看班级图片")
	@ApiImplicitParam(name = "查看班级图片", value = "查看班级图片  ")
	@GetMapping("/get/imgList")
	public Result<?> getImgList(String clazzId){
		Clazz clazz = clazzService.getById(clazzId);
		if (Objects.isNull(clazz)){
			return Result.error("不存在该班级");
		}
		if(StringUtils.isEmpty(clazz.getImages())){
			clazz.setImgList(new String[]{});
		}else {
			clazz.setImgList(clazz.getImages().split(","));
		}
		return Result.OK(clazz);
	}


}
